<template>
	<view >
		<view class='content'>
			<image class="logo" src="/static/logo.jpg"></image>
		</view>
		<view style='padding:10px 15%'>
			<u--input
			    placeholder="请输入登录账号"
			    border="surround"
			    v-model="username"
			  ></u--input>
		</view>
		<view style='padding:10px 15%'>
			<u--input
			    placeholder="请输入登录密码"
			    border="surround"
				type='password'
			    v-model="password"
			  ></u--input>
		</view>
		<view>
			<view style='padding:5px 15%;font-size: 14px;'>
				<u-row>
					<u-col span='1'>
						<u-radio-group v-model="chcek">
						    <u-radio
						      :name="1"
						    >
						    </u-radio>
						  </u-radio-group>
					</u-col>
					<u-col span='11'>
						<view>
							已阅读并同意
							<text style='color:#2979ff;text-align: center;' @click='toUrl("/pages/content/index?id=1")'>《用户服务协议》</text>
							和
							<text style='color:#2979ff;text-align: center;' @click='toUrl("/pages/content/index?id=2")'>《私隐政策》</text>
						</view>
					</u-col>
				</u-row>
			</view>
		</view>
		<view style='padding:20px 15%'>
			<u-row>
				<u-col span='6'>
					<view style='padding-right:10px' @click='toIndex'>
						<u-button text='返回' type='error' shape="circle" :plain='true'></u-button>
					</view>
				</u-col>
				<u-col span='6'>
					<view style='padding-left:10px' @click='login'>
						<u-button text='登录' type='success' shape="circle" :plain='true'></u-button>
					</view>
				</u-col>
			</u-row>
		</view>
		<u-line></u-line>
		<view class='content'>
			<view style='padding-top:10px' @click='wxLogin'>
				<u-icon name="weixin-fill" label='使用微信账号登录' color='#19be6b' labelColor='#909399' size='28'></u-icon>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				username: '',
				password: '',
				chcek: false
			}
		},
		onLoad() {

		},
		onShow() {

		},
		methods: {
			toIndex(){
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			toUrl(page){
				uni.navigateTo({
					url: page
				})
			},
			login(){
				if(this.password.trim() == '' || this.username.trim() == ''){
					uni.showToast({
						title: '请输入登录信息',
						icon: 'none'
					})
					return
				}
				this.request.post('api-login',{
					username: this.username,
					password: this.password
				}).then((res) => {
					console.log('oken',res.data)
					if(res.data){
						uni.setStorageSync('api_token', res.data);
						uni.switchTab({
							url: '/pages/index/index'
						})
					}else{
						uni.showToast({
							title: '账号/密码错误！',
							icon: 'none'
						})
					}
				})
			},
			wxLogin(){
				uni.showModal({
					title: '',
					content: '是否使用您的微信账号进行授权登录？',
					success: (data) => {
						if(data.confirm){
							let _this = this
							uni.login({
								provider: "weixin",
								success: function(res) {
									let data = {
										code: res.code
									}
									_this.request.get("wechat/mini/login", data).then((res) => {
										if (res.data.token) {
											uni.setStorageSync('api_token', res.data.token);
											uni.switchTab({
												url: '/pages/index/index'
											})
										}
									})
								},
							});
						}
					}
				})
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	
	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}
</style>